package de.hwrberlin.remo.diagram.custom.util;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;

import de.hwrberlin.remo.algorithm.AlgorithmListener;
import de.hwrberlin.remo.algorithm.InferenzAlgorithm;
import de.hwrberlin.remo.algorithm.steps.AlgorithmStep;
import de.hwrberlin.remo.model.KnowledgeBase;

public class AlgorithmExecutionJob implements IRunnableWithProgress,
	AlgorithmListener {

	private String algorithmName;
	private InferenzAlgorithm algorithm;
	private KnowledgeBase kbBase;
	
	private IProgressMonitor monitor;
	

	public AlgorithmExecutionJob(String algorithmName,
			InferenzAlgorithm algorithm, KnowledgeBase kbBase) {

		this.algorithmName = algorithmName;
		this.algorithm = algorithm;
		this.kbBase = kbBase;
	}

	@Override
	public void run(IProgressMonitor monitor) throws InvocationTargetException,
			InterruptedException {
		
		algorithm.registerListener(this);
		
		monitor.beginTask("Executing " + algorithmName, IProgressMonitor.UNKNOWN);
		this.monitor = monitor;
		
		algorithm.start(kbBase);
		algorithm.unregisterListener(this);
	}

	@Override
	public void stepFinished(AlgorithmStep step) {
		monitor.subTask(step.getLabel()); 
	}
}
